﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Xushre.WeiBoSDK {
    public partial class OClient2 {

        /// <summary>
        /// 获取当前登录用户的收藏列表
        /// </summary>
        /// <param name="count">单页返回的记录条数，默认为50。</param>
        /// <param name="page">返回结果的页码，默认为1。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string Favorites(int count = 50, int page = 1) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            data.Add("count", count);
            data.Add("page", page);

            try {
                response = Get(API.OClient2.Favorites.FAVORITES, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 获取当前用户的收藏列表的ID
        /// </summary>
        /// <param name="count">单页返回的记录条数，默认为50。</param>
        /// <param name="page">返回结果的页码，默认为1。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string FavoritesIds(int count = 50, int page = 1) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            data.Add("count", count);
            data.Add("page", page);

            try {
                response = Get(API.OClient2.Favorites.IDS, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 根据收藏ID获取指定的收藏信息
        /// </summary>
        /// <param name="id">需要查询的收藏ID。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string FavoritesShow(Int64 id) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            data.Add("id", id);

            try {
                response = Get(API.OClient2.Favorites.SHOW, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 根据标签获取当前登录用户该标签下的收藏列表
        /// </summary>
        /// <param name="tid">需要查询的标签ID。</param>
        /// <param name="count">单页返回的记录条数，默认为50。</param>
        /// <param name="page">返回结果的页码，默认为1。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string FavoritesByTags(Int64 tid, int count = 50, int page = 1) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            data.Add("tid", tid);
            data.Add("count", count);
            data.Add("page", page);

            try {
                response = Get(API.OClient2.Favorites.BY_TAGS, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 获取当前登录用户的收藏标签列表
        /// </summary>
        /// <param name="count">单页返回的记录条数，默认为10。</param>
        /// <param name="page">返回结果的页码，默认为1。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string FavoritesTags(int count = 10, int page = 1) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            data.Add("count", count);
            data.Add("page", page);

            try {
                response = Get(API.OClient2.Favorites.TAGS, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 获取当前用户某个标签下的收藏列表的ID
        /// </summary>
        /// <param name="tid">需要查询的标签ID。</param>
        /// <param name="count">单页返回的记录条数，默认为50。</param>
        /// <param name="page">返回结果的页码，默认为1。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string FavoritesByTagsIds(Int64 tid, int count = 50, int page = 1) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            data.Add("tid", tid);
            data.Add("count", count);
            data.Add("page", page);

            try {
                response = Get(API.OClient2.Favorites.BY_TAGS_IDS, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 添加一条微博到收藏里
        /// </summary>
        /// <param name="id">要收藏的微博ID。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string FavoritesCreate(Int64 id) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            data.Add("id", id);

            try {
                response = Post(API.OClient2.Favorites.CREATE, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 添加一条微博到收藏里
        /// </summary>
        /// <remarks>只能取消自己所收藏的微博</remarks>
        /// <param name="id">要收藏的微博ID。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string FavoritesDestory(Int64 id) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            data.Add("id", id);

            try {
                response = Post(API.OClient2.Favorites.DESTORY, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 根据收藏ID批量取消收藏
        /// </summary>
        /// <param name="ids">要取消收藏的收藏ID，最多不超过10个。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string FavoritesDestoryBatch(Int64[] ids) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            if (ids.Length > 10) {
                return "{\"error\":\"Parameter must be less than the number of ids 10.\",\"error_code\":9018}";
            }

            data.Add("ids", string.Join(",", ids));

            try {
                response = Post(API.OClient2.Favorites.DESTORY_BATCH, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 更新一条收藏的收藏标签
        /// </summary>
        /// <param name="id">需要更新的收藏ID。</param>
        /// <param name="tags">需要更新的标签内容。超过2条将自动去除。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string FavoritesTagsUpdate(Int64 id, string[] tags = null) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            if (tags != null && tags.Length > 0) {
                string[] temp = new string[2];
                for (int i = 0; i < tags.Length && i < temp.Length; i++) {
                    temp[i] = System.Web.HttpUtility.UrlEncode(tags[i]);
                }

                data.Add("tags", string.Join(",", temp));
            }

            data.Add("id", string.Join(",", id));

            try {
                response = Post(API.OClient2.Favorites.Tags.UPDATE, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 更新当前登录用户所有收藏下的指定标签
        /// </summary>
        /// <param name="id">需要更新的标签ID。</param>
        /// <param name="tag">需要更新的标签内容。不能为空。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string FavoritesTagsUpdateBatch(Int64 id, string tag) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            data.Add("tag", tag);
            data.Add("id", string.Join(",", id));

            try {
                response = Post(API.OClient2.Favorites.Tags.UPDATE_BATCH, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 删除当前登录用户所有收藏下的指定标签
        /// </summary>
        /// <param name="tid">需要删除的标签ID。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string FavoritesTagsDestoryBatch(Int64 tid) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            data.Add("tid", tid);

            try {
                response = Post(API.OClient2.Favorites.Tags.DESTORY_BATCH, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }
    }
}
